
$(function(){
});

var submitNo = 1;

function submitSynonymy(obj) {
	$("#submiting").show();
	$("#submit").hide();
	if(submitNo == 1) {
		// 第一次提交，如果词语中在系统中有重复项则返回重复的数据
		firstSubmit();
	} else {
		// 第二次提交，将重复的数据修改过后提交，如果还有重复则只能继续修改当前数据
		secondSubmit();
	}
	$("#submit").show();
	$("#submiting").hide();
}
function checkEnglishStd(engstd){
	if(engstd != null && engstd != undefined && engstd != ""){
		var Regx = /[\u4e00-\u9fa5]/;
		if (Regx.test(engstd)) {
			alert("英文标准词格式不对");
			return false;
		}
	}
	return true;
}
function secondSubmit() {
	var tabid = $("#tabid").val();
	if(tabid == "1_1") {
		var property = $("#editSynonymy select");
		for(var i in property) {
			if(!property[i]) {
				alert("请选择同义词属性");
				return false;
			}
		}
	}
	if(tabid = "21_7"){
		for(var i = 1;i<=numCount; i++){
			var engstd = $("#englishStd_"+i).val();
			if(!checkEnglishStd(engstd)){
				return;
			}
			var std = $("#std_"+i).val();
			if(!engstd && !std){
				alert("英文标准词和中文标准词必须填一个");
				return;
			}
		}
		
	}
	if(!checkSameWord()) {
		return false;
	}

	var formData = $('#editSynonymy').serialize();

	API.synonymyEditBatch(formData, function(res) {
		if (res.code == '0') {
			window.location.href = "/synonymy/synonymy_list.htm";
		} else {
			if(res.code == "40002") {
				alert(res.data.errMsg);
			} else {
				alert(res.desc);
			}
		}
	});
}

function firstSubmit() {
	var tabid = $("#tabid").val();
	var engstd = $("#englishStd_1").val();
	var std = $("#std_1").val();
	if(tabid == "1_1") {
		var property = $("select[name='property']").val();
		if(!property) {
			alert("请选择同义词属性");
			return false;
		}
	}
	if(tabid == "21_7"){
		if(!engstd && !std){
			alert("英文标准词和中文标准词必须填一个");
			return;
		}
	}
	if(engstd != null && engstd != undefined && engstd != ""){
		var Regx = /[\u4e00-\u9fa5]/;
		if (Regx.test(engstd)) {
			alert("英文标准词格式不对");
			return false;
		}
	}
	if(std != null && std != undefined && std != ""){
		var Regx = /[\u4e00-\u9fa5]/;
		if (!Regx.test(std)) {
			alert("中文标准词格式不对");
			return false;
		}
	}
	if(!checkSameWord()) {
		return false;
	}

	var formData = $('#editSynonymy').serialize();

	API.synonymyEdit(formData, function(res) {
		if (res.code == '0') {
			window.location.href = "/synonymy/synonymy_list.htm";
		} else {
			if(res.code == "40002") {
				submitNo = 2;
				
				$("#existSynonymy").show();
				var tableObj = $("#existSynonymy table");
				tableObj.append($(getExistHtml(res.data.existList)));
				
				alert(res.data.errMsg);
			} else if(res.code == "40016") {
				alert(res.data.errMsg);
			} else {
				alert(res.desc);
			}
		}
	});
}

function checkSameWord() {
	var words = $("#editSynonymy :text").not(".std").not(".englishStd");
	var wordsArray = [];
	var wordCount = 0;
	var inputName = '';
	for(var i=0;i<words.length;i++) {
		var wordObj = $(words[i]);
		var word = $.trim(wordObj.val());
		if(inputName != wordObj.attr("name")) {
			if(inputName && wordCount<2) {
				alert("一条同义词请至少添加两个词语");
				return false;
			}
			
			wordCount = 0;
			inputName = wordObj.attr("name");
		}
		if(word) {
			wordCount ++;
		}
	}
	if(wordCount < 2) {
		alert("一条同义词请至少添加两个词语");
		return false;
	}
	return true;
}
var numCount = 0 ;
function getExistHtml(existList) {
	var existHtml = '';
	var row, col;
	numCount = existList.length;
	for(var i=0;i<existList.length;i++) {
		var exist = existList[i];
		var num = i + 1;
		existHtml += '<table class="synonymy-edit">';
		existHtml += '<input type="hidden" name="id_' + num + '" value="' + exist.id + '"/>';
		if(exist.property) {
			existHtml += getPropertySelectAndStdTr(exist.property, exist.std, num,exist.englishStd);
		}
		existHtml += getWordInputTr(exist.wordArray, num);
		existHtml += '</table">';
	}
	return existHtml;
}

function getStdTd(std, num,englishStd) {
	var html = '<td class="std-display">中文标准词</td>';
	html += '<td class="std-display"><input type="text" class="std" name="std_' + (num) + '" id="std_' + (num) + '" style="width:98%" value="' + std + '" /></td>';
	html += '<td class="std-display">英文标准词</td>';
	html += '<td class="std-display"><input type="text" class="englishStd" name="englishStd_' + (num) + '" id="englishStd_' + (num) + '" style="width:98%" value="' + englishStd + '" /></td>';

	return html;
}

function getWordInputTr(wordArray, num) {
	var html = "";
	var col = 1;
	for(var i=0;i<wordArray.length;i++) {
		if(col == 1) {
			html += '<tr>';
		}
		html += '<td><input type="text" name="word_' + num;
		html += '" style="width:98%" value="' + wordArray[i];
		html += '" onkeyup="synonymyEditKeyUp(this);"></input></td>';
		col ++;
		if(col == 8) {
			html += '</tr>';
			col = 1;
		}
	}
	return html;
}

function getPropertySelectAndStdTr(property, std, num,englishStd) {
	var html = '<tr><td>属性</td><td><select name="property_' + num + '" style="width:80px;">';
	for(var i in propertyList) {
		html += '<option ';
		if(property == propertyList[i]) {
			html += 'selected';
		}
		html += '>' + propertyList[i] + '</option>';
	}
	html += '</select></td>';
	html += getStdTd(std, num,englishStd);
	html += '</tr>';
	return html;
}

function synonymyEditKeyUp(obj) {
	var _this = $(obj);
	var tdNode = _this.parent();
	var trNode = tdNode.parent();
	var value = _this.val();
	if(value) {
		addInput(_this, tdNode, trNode);
	} else {
		deleteInput(_this, tdNode, trNode);
	}
}

// 判断是否增加一个输入框
function addInput(obj, tdNode, trNode) {
	if(tdNode.next().html()) {
		return ;
	} else if(trNode.children().length == 8) {
		// 当前tr已有8个td，增加一个tr，查看是否有下个tr
		if(!trNode.next().html()) {
			trNode.parent().append('<tr><td><input type="text" name="' + obj.attr('name') + '" style="width:98%" onkeyup="synonymyEditKeyUp(this);"></input></td></tr>');
		}
	} else {
		// 当前tr不到8个td
		trNode.append('<td><input type="text" name="' + obj.attr('name') + '" style="width:98%" onkeyup="synonymyEditKeyUp(this);"></input></td>');
	}
}

// 判断是否删除一个输入框
function deleteInput(obj, tdNode, trNode) {
	if(tdNode.next().html()) {
		if(trNode.parent().find("input").length > 3) {
			tdNode.remove();
		}
	} else if(trNode.children().length == 8) {
		// 当前tr已有8个td，查询下一个tr中的td
		if(trNode.next().children().length==2) {
			trNode.next().remove();
		}
	}
}